//package com.itmk.config.security.detailservice;
//
//import com.itmk.config.security.exception.CustomerAuthenionException;
//import com.itmk.utils.ResultUtils;
//import com.itmk.web.sys_menu.entity.SysMenu;
//import com.itmk.web.sys_menu.service.SysMenuService;
//import com.itmk.web.sys_user.entity.SysUser;
//import com.itmk.web.sys_user.entity.UserInfo;
//import com.itmk.web.sys_user.service.SysUserService;
//import org.apache.commons.lang.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.core.GrantedAuthority;
//import org.springframework.security.core.authority.AuthorityUtils;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.security.core.userdetails.UsernameNotFoundException;
//import org.springframework.stereotype.Component;
//
//import java.util.ArrayList;
//import java.util.Arrays;
//import java.util.List;
//import java.util.Optional;
//import java.util.stream.Collectors;
//
//@Component("customerUserDetailService")
//public class CustomerUserDetailService implements UserDetailsService {
//    @Autowired
//    private SysUserService sysUserService;
//    @Autowired
//    private SysMenuService sysMenuService;
//    @Override
//    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        //认证
//        SysUser user =sysUserService.loadUser(username);
//        if(user == null){
//            throw new CustomerAuthenionException("用户名或密码错误！");
//        }
//        //查询代码权限
//        List<SysMenu> menuList = null;
//        List<String> collet = Optional.ofNullable(menuList).orElse(new ArrayList<>())
//                .stream()
//                .filter(item -> item != null && StringUtils.isNotEmpty(item.getCode()))
//                .map(item -> item.getCode())
//                .collect(Collectors.toList());
//        //判断是不是超级管理员
//        if(StringUtils.isNotEmpty(user.getIsAdmin())&&"1".equals(user.getIsAdmin())){
//            menuList = sysMenuService.list();
//            // 或者直接赋予所有权限
//            collet = Arrays.asList("sys:menu:add", "sys:menu:edit", "sys:menu:delete",
//                    "sys:role:assign","sys:role:add","sys:role:edit","sys:role:delete",
//                    "sys:department:add","sys:department:edit","sys:department:delete",
//                    "sys:sysNotice:add","sys:sysNotice:edit","sys:sysNotice:delete",
//                    "sys:news:add","sys:news:edit","sys:news:delete","sys:user:enabled",
//                    "sys:user:add","sys:user:edit","sys:user:delete",
//                    "sys:scheduleDetail:add","sys:scheduleDetail:edit","sys:scheduleDetail:delete",
//                    "sys:scheduleDetail:delList"
//            );
//        }else {
//            menuList = sysMenuService.getMenuByUserId(user.getUserId());
//        }
//        //权限交给springsecurity
//        String[] strings = collet.toArray(new String[collet.size()]);
//        List<GrantedAuthority> authorityList = AuthorityUtils.createAuthorityList(strings);
//        user.setAuthorities(authorityList);
//       return user;
//    }
//}
package com.itmk.config.security.detailservice;

import com.itmk.config.security.exception.CustomerAuthenionException;
import com.itmk.web.sys_menu.entity.SysMenu;
import com.itmk.web.sys_menu.service.SysMenuService;
import com.itmk.web.sys_user.entity.SysUser;
import com.itmk.web.sys_user.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


@Component("customerUserDetailService")
public class CustomerUserDetailService implements UserDetailsService {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysMenuService sysMenuService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser user = sysUserService.loadUser(username);
        if(user == null){
            throw new CustomerAuthenionException("用户名错误或账户不存在!");
        }
        List<SysMenu> menuList = null;
       List<String> collet = Optional.ofNullable(menuList).orElse(new ArrayList<>())
               .stream()
                .filter(item -> item != null && StringUtils.isNotEmpty(item.getCode()))
               .map(item -> item.getCode())
                .collect(Collectors.toList());
        //查询菜单权限
        //判断是否是超级管理员
        if (StringUtils.isNotEmpty(user.getIsAdmin()) && "1".equals(user.getIsAdmin())) {
            // 超级管理员直接赋予所有权限
            menuList = sysMenuService.list();
            //或者直接赋予所有权限
           collet = Arrays.asList("sys:menu:add", "sys:menu:edit", "sys:menu:delete",
                    "sys:role:assign","sys:role:add","sys:role:edit","sys:role:delete",
                    "sys:department:add","sys:department:edit","sys:department:delete",
                    "sys:sysNotice:add","sys:sysNotice:edit","sys:sysNotice:delete",
                    "sys:news:add","sys:news:edit","sys:news:delete","sys:user:enabled",
                    "sys:user:add","sys:user:edit","sys:user:delete",
                    "sys:scheduleDetail:add","sys:scheduleDetail:edit","sys:scheduleDetail:delete",
                    "sys:scheduleDetail:delList","sys:patient:enable","sys:patient:delete",
                   "sys:patient:reset","sys:makeOrder:delete","sys:makeOrder:call",
                   "sys:visit:edit"
            );
            System.out.println("User isAdmin: " + user.getIsAdmin());
            System.out.println("Permissions: " + collet);
        } else {
            menuList = sysMenuService.getMenuByUserId(user.getUserId());
            //获取菜单表的code字段
            List<String> collect = Optional.ofNullable(menuList).orElse(new ArrayList<>())
                    .stream()
                    .filter(item -> item != null && StringUtils.isNotEmpty(item.getCode()))
                    .map(item -> item.getCode())
                    .collect(Collectors.toList());
            //把权限字段交给springsecurity进行管理
            String[] strings = collect.toArray(new String[collect.size()]);
            List<GrantedAuthority> authorityList = AuthorityUtils.createAuthorityList(strings);
            user.setAuthorities(authorityList);
        }
        //权限交给springsecurity
        String[] strings = collet.toArray(new String[collet.size()]);
       List<GrantedAuthority> authorityList = AuthorityUtils.createAuthorityList(strings);
        user.setAuthorities(authorityList);
       return user;
    }
}
